#tag Class
Class NSData
Inherits NSObject
	#tag Method, Flags = &h21
		Private Shared Function ClassRef() As Ptr
		  
		  static ref as Ptr = Cocoa.NSClassFromString("NSData")
		  return ref
		  
		End Function
	#tag EndMethod

	#tag Method, Flags = &h1000
		Sub Constructor(file as FolderItem)
		  
		  #if targetMacOS
		    declare function initWithContentsOfFile lib CocoaLib selector "initWithContentsOfFile:" (obj_id as Ptr, path as CFStringRef) as Ptr
		    
		    super.Constructor(initWithContentsOfFile(Allocate("NSData"), file.POSIXPath), NSData.hasOwnership)
		    
		  #else
		    #pragma unused file
		  #endif
		  
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h1000
		Sub Constructor(dataBlock as MemoryBlock, length as UInt32 = 0)
		  
		  #if targetMacOS
		    declare function initWithBytes lib CocoaLib selector "initWithBytes:length:" (obj_id as Ptr, bytes as Ptr, length as UInt32) as Ptr
		    
		    dim dataLen as UInt32
		    if length > 0 then
		      dataLen = length
		    else
		      dataLen = dataBlock.Size
		    end if
		    
		    super.Constructor(initWithBytes(Allocate("NSData"), dataBlock, dataLen), NSData.hasOwnership)
		    
		  #else
		    #pragma unused dataBlock
		    #pragma unused length
		  #endif
		  
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h1000
		Sub Constructor(aData as NSData)
		  
		  #if targetMacOS
		    declare function initWithData lib CocoaLib selector "initWithData:" (obj_id as Ptr, aData as Ptr) as Ptr
		    
		    super.Constructor(initWithData(Allocate("NSData"), aData), NSData.hasOwnership)
		    
		  #else
		    #pragma unused aData
		  #endif
		  
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h1000
		Sub Constructor(aURL as NSURL)
		  
		  #if targetMacOS
		    declare function initWithContentsOfURL lib CocoaLib selector "initWithContentsOfURL:" (obj_id as Ptr, aURL as Ptr) as Ptr
		    
		    super.Constructor(initWithContentsOfURL(Allocate("NSData"), aURL), NSData.hasOwnership)
		    
		  #else
		    #pragma unused aURL
		  #endif
		  
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h1000
		Sub Constructor(dataString as String)
		  
		  self.Constructor(dataString, dataString.lenB)
		  
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h1000
		 Shared Function Create() As NSData
		  
		  #if TargetMacOS
		    declare function data_ lib CocoaLib selector "data" (class_id as Ptr) as Ptr
		    
		    dim dataRef as Ptr = data_(ClassRef)
		    if dataRef <> nil then
		      return new NSData(dataRef)
		    end if
		    
		  #endif
		  
		End Function
	#tag EndMethod

	#tag Method, Flags = &h1000
		 Shared Function CreateWithData(aData as NSData) As NSData
		  
		  #if TargetMacOS
		    declare function dataWithData lib CocoaLib selector "dataWithData:" (class_id as Ptr, aData as Ptr) as Ptr
		    
		    if aData <> nil then
		      dim dataRef as Ptr = dataWithData(ClassRef, aData)
		      if dataRef <> nil then
		        return new NSData(dataRef)
		      end if
		    end if
		    
		  #else
		    #pragma unused aData
		  #endif
		  
		End Function
	#tag EndMethod

	#tag Method, Flags = &h1000
		 Shared Function CreateWithFile(file as FolderItem) As NSData
		  
		  #if TargetMacOS
		    declare function dataWithContentsOfFile lib CocoaLib selector "dataWithContentsOfFile:" (class_id as Ptr, path as CFStringRef) as Ptr
		    
		    if file <> nil then
		      dim dataRef as Ptr = dataWithContentsOfFile(ClassRef, file.POSIXPath)
		      if dataRef <> nil then
		        return new NSData(dataRef)
		      end if
		    end if
		    
		  #else
		    #pragma unused file
		  #endif
		  
		End Function
	#tag EndMethod

	#tag Method, Flags = &h1000
		 Shared Function CreateWithMemoryBlock(dataBlock as MemoryBlock, length as UInt32) As NSData
		  
		  #if TargetMacOS
		    declare function dataWithBytes lib CocoaLib selector "dataWithBytes:length:" (class_id as Ptr, bytes as Ptr, length as UInt32) as Ptr
		    
		    if dataBlock <> nil then
		      
		      dim dataLen as UInt32
		      if length > 0 then
		        dataLen = length
		      else
		        dataLen = dataBlock.Size
		      end if
		      
		      dim dataRef as Ptr = dataWithBytes(ClassRef, dataBlock, dataLen)
		      if dataRef <> nil then
		        return new NSData(dataRef)
		      end if
		    end if
		    
		  #else
		    #pragma unused dataBlock
		    #pragma unused length
		  #endif
		  
		End Function
	#tag EndMethod

	#tag Method, Flags = &h1000
		 Shared Function CreateWithString(dataString as String) As NSData
		  
		  return NSData.CreateWithMemoryBlock(dataString, dataString.lenB)
		End Function
	#tag EndMethod

	#tag Method, Flags = &h1000
		 Shared Function CreateWithURL(aURL as NSURL) As NSData
		  
		  #if TargetMacOS
		    declare function dataWithContentsOfURL lib CocoaLib selector "dataWithContentsOfURL:" (class_id as Ptr, aURL as Ptr) as Ptr
		    
		    if aURL <> nil then
		      dim dataRef as Ptr = dataWithContentsOfURL(ClassRef, aURL)
		      if dataRef <> nil then
		        return new NSData(dataRef)
		      end if
		    end if
		    
		  #else
		    #pragma unused aURL
		  #endif
		  
		End Function
	#tag EndMethod

	#tag Method, Flags = &h1000
		Function Data() As MemoryBlock
		  
		  #if targetMacOS
		    declare function bytes lib CocoaLib selector "bytes" (obj_id as Ptr) as Ptr
		    
		    if self.Length > 0 then
		      return bytes(self)
		    end if
		    
		  #endif
		  
		End Function
	#tag EndMethod

	#tag Method, Flags = &h1000
		Function Data(range as Cocoa.NSRange) As MemoryBlock
		  
		  #if targetMacOS
		    declare function getBytes lib CocoaLib selector "getBytes:range:" (obj_id as Ptr, range as Cocoa.NSRange) as Ptr
		    
		    return getBytes(self, range)
		    
		  #else
		    #pragma unused range
		  #endif
		  
		End Function
	#tag EndMethod

	#tag Method, Flags = &h1000
		Function Data(length as UInt32) As MemoryBlock
		  
		  #if targetMacOS
		    declare function getBytes lib CocoaLib selector "getBytes:length:" (obj_id as Ptr, length as UInt32) as Ptr
		    
		    return getBytes(self, length)
		    
		  #else
		    #pragma unused length
		  #endif
		  
		End Function
	#tag EndMethod

	#tag Method, Flags = &h1000
		Function IsEqual(otherData as NSData) As Boolean
		  
		  #if targetMacOS
		    declare function isEqualToData lib CocoaLib selector "isEqualToData:" (obj_id as Ptr, otherData as Ptr) as Boolean
		    
		    dim dataRef as Ptr
		    if otherData <> nil then
		      dataRef = otherData
		    end if
		    
		    return isEqualToData(self, dataRef)
		    
		  #else
		    #pragma unused otherData
		  #endif
		  
		End Function
	#tag EndMethod

	#tag Method, Flags = &h1000
		Function Length() As UInt32
		  
		  #if targetMacOS
		    declare function length lib CocoaLib selector "length" (obj_id as Ptr) as UInt32
		    
		    return length(self)
		    
		  #endif
		  
		End Function
	#tag EndMethod

	#tag Method, Flags = &h1000
		Function RangeOfData(dataToFind as NSData, options as UInt32, range as Cocoa.NSRange) As Cocoa.NSRange
		  
		  #if targetMacOS
		    declare function rangeOfData lib CocoaLib selector "rangeOfData:options:range:" _
		    (obj_id as Ptr, dataToFind as Ptr, options as UInt32, range as Cocoa.NSRange) as Cocoa.NSRange
		    
		    dim dataRef as Ptr
		    if dataToFind <> nil then
		      dataRef = dataToFind
		    end if
		    
		    return rangeOfData(self, dataRef, options, range)
		    
		  #else
		    #pragma unused dataToFind
		    #pragma unused options
		    #pragma unused range
		  #endif
		  
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function StringValue() As String
		  
		  return self.Data
		  
		End Function
	#tag EndMethod

	#tag Method, Flags = &h1000
		Function Subdata(range as Cocoa.NSRange) As NSData
		  
		  #if targetMacOS
		    declare function subdataWithRange lib CocoaLib selector "subdataWithRange:" (obj_id as Ptr, range as Cocoa.NSRange) as Ptr
		    
		    dim dataRef as Ptr = subdataWithRange(self, range)
		    if dataRef <> nil then
		      return new NSData(dataRef)
		    end if
		    
		  #else
		    #pragma unused range
		  #endif
		  
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function VariantValue() As Variant
		  
		  return self.Data
		  
		End Function
	#tag EndMethod

	#tag Method, Flags = &h1000
		Function WriteToFile(file as FolderItem, atomically as Boolean) As Boolean
		  
		  #if targetMacOS
		    declare function writeToFile lib CocoaLib selector "writeToFile:atomically:" (obj_id as Ptr, path as CFStringRef, flag as Boolean) as Boolean
		    
		    if file <> nil then
		      return writeToFile(self, file.POSIXPath, atomically)
		    end if
		    
		  #else
		    #pragma unused file
		    #pragma unused atomically
		  #endif
		  
		End Function
	#tag EndMethod


	#tag Constant, Name = NSDataSearchAnchored, Type = Double, Dynamic = False, Default = \"2", Scope = Public
	#tag EndConstant

	#tag Constant, Name = NSDataSearchBackwards, Type = Double, Dynamic = False, Default = \"1", Scope = Public
	#tag EndConstant


	#tag ViewBehavior
		#tag ViewProperty
			Name="Description"
			Group="Behavior"
			Type="String"
			EditorType="MultiLineEditor"
			InheritedFrom="NSObject"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Index"
			Visible=true
			Group="ID"
			InitialValue="-2147483648"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Left"
			Visible=true
			Group="Position"
			InitialValue="0"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Name"
			Visible=true
			Group="ID"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Super"
			Visible=true
			Group="ID"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Top"
			Visible=true
			Group="Position"
			InitialValue="0"
			InheritedFrom="Object"
		#tag EndViewProperty
	#tag EndViewBehavior
End Class
#tag EndClass
